<?php

defined('BY_JYA') or exit('error');
list($params)=$arguments;
		$card_type = $params['card_type'];
		$logo_url = $params['wxlogourl'];
		
		$brand_name = $params['brand_name'];
		$code_type = 'CODE_TYPE_NONE';
		$title = $params['title'];
		//$color = $params['color'];

		//if (empty($color)) {
			$color = 'Color010';
		//}

		$notice = $params['notice'];
		$service_phone = $params['service_phone'];
		$description = $params['description'];
		$type = $params['datetype'];

		if ($type == '1') {
			$begin_timestamp = $params['begin_timestamp'];
			$end_timestamp = $params['end_timestamp'];
		}
		else {
			if ($type == '0') {
				$fixed_term = (empty($params['fixed_term']) ? 0 : $params['fixed_term']);
				$fixed_begin_term = (empty($params['fixed_begin_term']) ? 0 : $params['fixed_begin_term']);
			}
		}

		$quantity = (empty($params['quantity']) ? 100 : $params['quantity']);
		$use_limit = (empty($params['use_limit']) ? 1 : $params['use_limit']);
		$get_limit = (empty($params['get_limit']) ? 1 : $params['get_limit']);
		$use_custom_code = 'false';
		$bind_openid = 'false';
		$can_share = (empty($params['can_share']) ? 'false' : 'true');
		$can_give_friend = (empty($params['can_give_friend']) ? 'false' : 'true');
		$location_id_list = '';
		$center_title = $params['center_title'];
		$center_sub_title = $params['center_sub_title'];
		if (strpos($params['center_url'], 'http://')) {
			$center_url = str_replace('http://', '', $params['center_url']);
		}

		if (strpos($params['center_url'], 'https://')) {
			$center_url = str_replace('https://', '', $params['center_url']);
		}
		//$center_url = $this->checkurl($params['center_url']);
		$setcustom = $params['setcustom'];

		if (!empty($setcustom)) {
			$custom_url_name = $params['custom_url_name'];
			$custom_url_sub_title = $params['custom_url_sub_title'];
			if (strpos($params['custom_url'], 'http://')) {
				$custom_url = str_replace('http://', '', $params['custom_url']);
			}

			if (strpos($params['custom_url'], 'https://')) {
				$custom_url = str_replace('https://', '', $params['custom_url']);
			}
				//$custom_url = $this->checkurl($params['custom_url']);
		}

		$setpromotion = $params['setpromotion'];

		if (!empty($setpromotion)) {
			$promotion_url_name = $params['promotion_url_name'];
			$promotion_url_sub_title = $params['promotion_url_sub_title'];
			//$promotion_url = $this->checkurl($params['promotion_url']);
			if (strpos($params['promotion_url'], 'http://')) {
			$promotion_url = str_replace('http://', '', $params['promotion_url']);
		}

		if (strpos($params['promotion_url'], 'https://')) {
			$promotion_url = str_replace('https://', '', $params['promotion_url']);
		}
		}

		$source = '';
		$can_use_with_other_discount = (empty($params['can_use_with_other_discount']) ? 'false' : 'true');
		$setabstract = $params['abstractimglocalpath'];
		$abstract = $params['abstract'];
		$icon_url_list = $params['icon_url_list'];
		$text_image_list = $params['text_image_list'];
		$time_limit = '';
		$business_service = '';
		$business = array();

		if (!empty($params['BIZ_SERVICE_FREE_WIFI'])) {
			$business[] = '"BIZ_SERVICE_FREE_WIFI"';
		}

		if (!empty($params['BIZ_SERVICE_WITH_PET'])) {
			$business[] = '"BIZ_SERVICE_WITH_PET"';
		}

		if (!empty($params['BIZ_SERVICE_FREE_PARK'])) {
			$business[] = '"BIZ_SERVICE_FREE_PARK"';
		}

		if (!empty($params['BIZ_SERVICE_DELIVER'])) {
			$business[] = '"BIZ_SERVICE_DELIVER"';
		}

		if (!empty($business)) {
			$business_service = implode(',', $business);
		}

		if ($card_type == 'CASH') {
			$accept_category = $params['accept_category'];
			$reject_category = $params['reject_category'];
			$least_cost = (empty($params['least_cost']) ? 0 : $params['least_cost']);
			$reduce_cost = (empty($params['reduce_cost']) ? 0 : $params['reduce_cost']);
		}
		else if ($card_type == 'DISCOUNT') {
			$discount = (empty($params['discount']) ? 0 : $params['discount']);
		}
		else if ($card_type == 'MEMBER_CARD') {
			$background_pic_url = $params['background_pic_url'];
			$supply_bonus = $params['supply_bonus'];
			$supply_balance = $params['supply_balance'];
			$bonus_url = $params['bonus_url'];
			$balance_url = $params['balance_url'];
			$prerogative = $params['prerogative'];
			$auto_activate = $params['auto_activate'];
			$activate_url = $params['activate_url'];
			$custom_field1 = $params['custom_field1'];

			if (!empty($custom_field1)) {
				$custom_field_name_type1 = $params['custom_field_name_type1'];
				$custom_field_url1 = $params['custom_field_url1'];
			}

			$custom_field2 = $params['custom_field2'];

			if (!empty($custom_field2)) {
				$custom_field_name_type2 = $params['custom_field_name_type2'];
				$custom_field_url2 = $params['custom_field_url2'];
			}

			$custom_field3 = $params['custom_field3'];

			if (!empty($custom_field3)) {
				$custom_field_name_type3 = $params['custom_field_name_type3'];
				$custom_field_url3 = $params['custom_field_url3'];
			}

			$custom_cell1 = $params['custom_cell1'];

			if (!empty($custom_cell1)) {
				$custom_cell1_name = $params['custom_cell1_name'];
				$custom_cell1_tips = $params['custom_cell1_tips'];
				$custom_cell1_url = $params['custom_cell1_url'];
			}
		}
		else {
			return false;
		}

		$jsonData = '{';
		$jsonData .= '"card":{';
		$jsonData .= '"card_type":"' . $card_type . '"';

		if ($card_type == 'CASH') {
			$jsonData .= ',"cash":{';
		}
		else if ($card_type == 'DISCOUNT') {
			$jsonData .= ',"discount":{';
		}
		else {
			if ($card_type == 'MEMBER_CARD') {
				$jsonData .= ',"member_card":{';

				if (!empty($background_pic_url)) {
					$jsonData .= '"background_pic_url":"' . $background_pic_url . '",';
				}
			}
		}

		$jsonData .= '"base_info":{';
		$jsonData .= '"logo_url":"' . $logo_url . '"';
		$jsonData .= ',"brand_name":"' . $brand_name . '"';
		$jsonData .= ',"code_type":"' . $code_type . '"';
		$jsonData .= ',"title":"' . $title . '"';
		$jsonData .= ',"color":"' . $color . '"';
		$jsonData .= ',"notice":"' . $notice . '"';

		if (!empty($service_phone)) {
			$jsonData .= ',"service_phone":"' . $service_phone . '"';
		}

		$jsonData .= ',"description":"' . $description . '"';
		$jsonData .= ',"date_info":{';

		if ($type == '1') {
			$jsonData .= '"type":"DATE_TYPE_FIX_TIME_RANGE"';
			$jsonData .= ',"begin_timestamp":' . $begin_timestamp;
			$jsonData .= ',"end_timestamp":' . $end_timestamp;
		}
		else if ($type == '0') {
			$jsonData .= '"type":"DATE_TYPE_FIX_TERM"';
			$jsonData .= ',"fixed_term":' . $fixed_term;
			$jsonData .= ',"fixed_begin_term":' . $fixed_begin_term;
		}
		else {
			if ($type == 'DATE_TYPE_PERMANENT') {
				$jsonData .= '"type":"DATE_TYPE_PERMANENT"';
			}
		}

		$jsonData .= '}';
		$jsonData .= ',"sku":{"quantity":' . $quantity . '}';
		$jsonData .= ',"use_limit":' . $use_limit;
		$jsonData .= ',"get_limit":' . $get_limit;
		$jsonData .= ',"can_share":' . $can_share;
		$jsonData .= ',"can_give_friend":' . $can_give_friend;
		$jsonData .= ',"center_title":"' . $center_title . '"';
		$jsonData .= ',"center_sub_title":"' . $center_sub_title . '"';
		$jsonData .= ',"center_url":"' . $center_url . '"';

		if (!empty($setcustom)) {
			$jsonData .= ',"custom_url_name":"' . $custom_url_name . '"';
			$jsonData .= ',"custom_url":"' . $custom_url . '"';
			$jsonData .= ',"custom_url_sub_title":"' . $custom_url_sub_title . '"';
		}

		if (!empty($setpromotion)) {
			$jsonData .= ',"promotion_url_name":"' . $promotion_url_name . '"';
			$jsonData .= ',"promotion_url_sub_title":"' . $promotion_url_sub_title . '"';
			$jsonData .= ',"promotion_url":"' . $promotion_url . '"';
		}

		$jsonData .= '}';
		$jsonData .= ',"advanced_info":{';
		$jsonData .= '"use_condition":{';

		if (!empty($accept_category)) {
			$jsonData .= '"accept_category":"' . $accept_category . '",';
		}

		if (!empty($reject_category)) {
			$jsonData .= '"reject_category":"' . $reject_category . '",';
		}

		$jsonData .= '"can_use_with_other_discount":' . $can_use_with_other_discount;
		$jsonData .= '}';

		if (!empty($icon_url_list)) {
			$jsonData .= ',"abstract":{';
			$jsonData .= '"abstract":"' . $abstract . '"';
			$jsonData .= ',"icon_url_list":["' . $icon_url_list . '"]';
			if (is_array($text_image_list) && !empty($text_image_list)) {
				$jsonData .= ',"text_image_list":[';
				$listnum = 0;

				foreach ($text_image_list as $text_image) {
					if (0 < $listnum) {
						$jsonData .= ',';
					}

					$jsonData .= '{';
					$jsonData .= '"image_url":"' . $text_image['image_url'] . '"';
					$jsonData .= ',"text":"' . $text_image['text'] . '"';
					$jsonData .= '}';
					++$listnum;
				}

				$jsonData .= ']';
			}

			$jsonData .= '}';
		}

		if (!empty($business_service)) {
			$jsonData .= ',"business_service":[';
			$jsonData .= $business_service;
			$jsonData .= ']';
		}

		$jsonData .= '}';

		if ($card_type == 'CASH') {
			$jsonData .= ',"least_cost":"' . $least_cost . '"';
			$jsonData .= ',"reduce_cost":"' . $reduce_cost . '"';
		}
		else if ($card_type == 'DISCOUNT') {
			$jsonData .= ',"discount":"' . $discount . '"';
		}
		else {
			if ($card_type == 'MEMBER_CARD') {
				$jsonData .= ',"supply_bonus":' . $supply_bonus;

				if (!empty($bonus_url)) {
					$jsonData .= ',"bonus_url":"' . $bonus_url . '"';
				}

				$jsonData .= ',"supply_balance":' . $supply_balance;

				if (!empty($balance_url)) {
					$jsonData .= ',"balance_url":"' . $balance_url . '"';
				}

				$jsonData .= ',"prerogative":"' . $prerogative . '"';
				$jsonData .= ',"auto_activate":' . $auto_activate;

				if (!empty($custom_field1)) {
					$jsonData .= ',"custom_field1":{';
					$jsonData .= '"name_type":"' . $custom_field_name_type1 . '"';

					if (!empty($custom_field_url1)) {
						$jsonData .= ',"url":"' . $custom_field_url1 . '"';
					}

					$jsonData .= '}';
				}

				if (!empty($custom_field2)) {
					$jsonData .= ',"custom_field2":{';
					$jsonData .= '"name_type":"' . $custom_field_name_type2 . '"';

					if (!empty($custom_field_url2)) {
						$jsonData .= ',"url":"' . $custom_field_url2 . '"';
					}

					$jsonData .= '}';
				}

				if (!empty($custom_field3)) {
					$jsonData .= ',"custom_field3":{';
					$jsonData .= '"name_type":"' . $custom_field_name_type3 . '"';

					if (!empty($custom_field_url3)) {
						$jsonData .= ',"url":"' . $custom_field_url3 . '"';
					}

					$jsonData .= '}';
				}

				if (!empty($activate_url)) {
					$jsonData .= ',"activate_url":"' . $activate_url . '"';
				}

				if (!empty($custom_cell1)) {
					$jsonData .= ',"custom_cell1":{';
					$jsonData .= '"name":"' . $custom_cell1_name . '"';
					$jsonData .= ',"tips":"' . $custom_cell1_tips . '"';
					$jsonData .= ',"url":"' . $custom_cell1_url . '"';
					$jsonData .= '}';
				}
			}
		}

		$jsonData .= '}';
		$jsonData .= '}';
		$jsonData .= '}';		
		$token =  cfc('account')->getAccessToken();		
		$url = 'https://api.weixin.qq.com/card/create?access_token=' . $token;
		$result = cfc('http')->ihttp_post($url, $jsonData);
	
		$result =@json_decode($result['content'],true);
		return $result;
